package util;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

abstract public class AbstractServlet extends HttpServlet {

	private static final long serialVersionUID = -6833214490730874938L;

	abstract protected void doGetAbstract(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
	abstract protected void doPostAbstract(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setHeader("Cache-Control","no-cache"); //HTTP 1.1
		resp.setHeader("Pragma","no-cache"); //HTTP 1.0
		resp.setDateHeader ("Expires", -1); //evita o caching no servidor proxy
		
		if (req.getSession().getAttribute("usuarioLogado") == null) {
			resp.sendRedirect(getServletContext().getContextPath() + "/index.jsp");
		} else {
			doGetAbstract(req, resp);
		}
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)	throws ServletException, IOException {
		resp.setHeader("Cache-Control","no-cache"); //HTTP 1.1
		resp.setHeader("Pragma","no-cache"); //HTTP 1.0
		resp.setDateHeader ("Expires", -1); //evita o caching no servidor proxy
		
		if (req.getSession().getAttribute("usuarioLogado") == null) {
			resp.sendRedirect(getServletContext().getContextPath() + "/index.jsp");
		} else {
			doPostAbstract(req, resp);
		}
	}
}